package com.jnu.hospitalexamination.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jnu.hospitalexamination.common.Result;
import com.jnu.hospitalexamination.entity.*;
import com.jnu.hospitalexamination.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealdetailedService setmealdetailedService;

    @Autowired
    private CheckitemService checkitemService;

    @RequestMapping("/list")
    public Result getSetmealListbytype(Integer type) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getType, type);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        if (setmealList.isEmpty()) {
            return Result.error(500, "相关套餐不存在", null);
        }
        return Result.success(200, "套餐查询成功", setmealList);
    }

    @RequestMapping("/getCheckitem")
    public Result getSetmealById(@RequestParam Integer smid) {
        LambdaQueryWrapper<Setmealdetailed> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmealdetailed::getSmid, smid);
        List<Setmealdetailed> setmealdetailedList = setmealdetailedService.list(queryWrapper1);
        List<Checkitem> checkitemList = new ArrayList<>();

        for( Setmealdetailed setmealdetailed : setmealdetailedList){
            Checkitem checkitem = checkitemService.getById(setmealdetailed.getCiid());
            checkitemList.add(checkitem);
        }
        return Result.success(200, "查询成功", checkitemList);
    }

    @RequestMapping("/getPrice")
    public Result getPrice(@RequestParam Integer smId) {
        try {
            Setmeal setmeal = setmealService.getById(smId);
            return Result.success(200, "查询成功", setmeal.getPrice());
        } catch (Exception e) {
            return Result.error(500, "套餐不存在", null);
        }
    }

}
